#!/usr/bin/env ruby
require 'optparse'
options = {}
optparse = OptionParser.new do |opts|
    opts.banner = 'usage: clean [options]'
    options[:src] = nil
    opts.on('-s', '--src SRC', 'the source dir') do |src|
        options[:src] = src
    end
    options[:dst] = nil
    opts.on('-d', '--dst DST', 'the destination dir') do |dst|
        options[:dst] = dst
    end
    options[:mtime] = nil
    opts.on('-m', '--mtime MTIME', 'the last modify time') do |mtime|
        options[:mtime] = mtime 
    end
end
optparse.parse!
puts optparse.help unless \
         options[:src] and options[:dst] and options[:mtime]
src, dst, mtime = options[:src], options[:dst], options[:mtime]
['src', 'dst'].each do |item|
    eval "#{item} = #{item}.end_with?('/') ? #{item} : #{item} + '/'"
end
src_dir_files = Array.new
dst_dir_files = Array.new
['src', 'dst'].each do |item|
    eval "
    Dir.open(#{item}).each do |f|
        #{item}_dir_files << f if File.file? #{item} + f
    end
    "
end
secs = mtime.to_f * 24 * 60 * 60
(src_dir_files & dst_dir_files).each do |f|
    if File.size(src + f) == File.size(dst + f) and \
                           Time.now - File.mtime(src + f) > secs
        puts "deleteing #{src + f}"
        File.delete(src + f)
    end
end

